<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use App\Models\Comment;
use App\Models\AdminUser;
use App\Guards\AdminGuard;

class CommentController extends Controller {

    public function __construct() {
        $this->middleware('need_admin_login');
    }

    public function comments() {
        if(AdminGuard::is_superadmin(request()->admin_user)){
            $comments = Comment::whereExists(function ($query){
                            $query->select(DB::raw(1))->from('posts')
                                  ->whereRaw('posts.id=comments.post_id and posts.deleted_at is NULL');
                        })->paginate(20);
        }else{
            $comments = Comment::whereExists(function ($query){
                            $query->select(DB::raw(1))->from('posts')
                                  ->whereRaw('posts.luo_id=' . request()->admin_user->luo_id . ' and posts.id=comments.post_id and posts.deleted_at is NULL');
                        })->paginate(20);
        }
        return view('admin.comments.list', ['comments' => $comments]);
    }

    public function delete($id) {
        $comment = Comment::findOrFail($id);
        if(AdminGuard::can_admin_manage_comment(request()->admin_user, $comment)){
            $comment->delete();
        }
    }
}
